/*   1:    */ package com.atomikos.icatch.jta;
/*   2:    */ 
/*   3:    */ import com.atomikos.datasource.RecoverableResource;
/*   4:    */ import com.atomikos.datasource.ResourceException;
/*   5:    */ import com.atomikos.datasource.TransactionalResource;
/*   6:    */ import com.atomikos.datasource.xa.TemporaryXATransactionalResource;
/*   7:    */ import com.atomikos.datasource.xa.XAResourceTransaction;
/*   8:    */ import com.atomikos.datasource.xa.XATransactionalResource;
/*   9:    */ import com.atomikos.icatch.CompositeTerminator;
/*  10:    */ import com.atomikos.icatch.CompositeTransaction;
/*  11:    */ import com.atomikos.icatch.HeurHazardException;
/*  12:    */ import com.atomikos.icatch.HeurMixedException;
/*  13:    */ import com.atomikos.icatch.HeurRollbackException;
/*  14:    */ import com.atomikos.icatch.SysException;
/*  15:    */ import com.atomikos.icatch.TransactionControl;
/*  16:    */ import com.atomikos.icatch.TxState;
/*  17:    */ import com.atomikos.icatch.system.Configuration;
/*  18:    */ import java.util.Collection;
/*  19:    */ import java.util.Enumeration;
/*  20:    */ import java.util.HashMap;
/*  21:    */ import java.util.Iterator;
/*  22:    */ import java.util.Map;
/*  23:    */ import java.util.Stack;
/*  24:    */ import javax.transaction.HeuristicMixedException;
/*  25:    */ import javax.transaction.HeuristicRollbackException;
/*  26:    */ import javax.transaction.Synchronization;
/*  27:    */ import javax.transaction.SystemException;
/*  28:    */ import javax.transaction.Transaction;
/*  29:    */ import javax.transaction.xa.XAException;
/*  30:    */ import javax.transaction.xa.XAResource;
/*  31:    */ 
/*  32:    */ class TransactionImp
/*  33:    */   implements Transaction
/*  34:    */ {
/*  35:    */   protected CompositeTransaction ct_;
/*  36:    */   protected Map xaresToTxMap_;
/*  37:    */   protected Stack suspendstack_;
/*  38:    */   protected boolean autoRegistration_;
/*  39:    */   
/*  40:    */   static void rethrowAsJtaRollbackException(String msg, Throwable cause)
/*  41:    */     throws javax.transaction.RollbackException
/*  42:    */   {
/*  43: 70 */     javax.transaction.RollbackException ret = new javax.transaction.RollbackException(msg);
/*  44: 71 */     ret.initCause(cause);
/*  45: 72 */     throw ret;
/*  46:    */   }
/*  47:    */   
/*  48:    */   static void rethrowAsJtaHeuristicMixedException(String msg, Throwable cause)
/*  49:    */     throws HeuristicMixedException
/*  50:    */   {
/*  51: 77 */     HeuristicMixedException ret = new HeuristicMixedException(msg);
/*  52: 78 */     ret.initCause(cause);
/*  53: 79 */     throw ret;
/*  54:    */   }
/*  55:    */   
/*  56:    */   static void rethrowAsJtaHeuristicRollbackException(String msg, Throwable cause)
/*  57:    */     throws HeuristicRollbackException
/*  58:    */   {
/*  59: 84 */     HeuristicRollbackException ret = new HeuristicRollbackException(msg);
/*  60: 85 */     ret.initCause(cause);
/*  61: 86 */     throw ret;
/*  62:    */   }
/*  63:    */   
/*  64:    */   TransactionImp(CompositeTransaction ct, boolean autoRegistration)
/*  65:    */   {
/*  66:111 */     this.ct_ = ct;
/*  67:112 */     this.autoRegistration_ = autoRegistration;
/*  68:113 */     this.xaresToTxMap_ = new HashMap();
/*  69:    */   }
/*  70:    */   
/*  71:    */   void setSuspendedStack(Stack suspendstack)
/*  72:    */   {
/*  73:118 */     this.suspendstack_ = suspendstack;
/*  74:    */   }
/*  75:    */   
/*  76:    */   CompositeTransaction getCT()
/*  77:    */   {
/*  78:123 */     return this.ct_;
/*  79:    */   }
/*  80:    */   
/*  81:    */   Stack getSuspendedStack()
/*  82:    */   {
/*  83:128 */     return this.suspendstack_;
/*  84:    */   }
/*  85:    */   
/*  86:    */   private void printMsg(String msg, int level)
/*  87:    */   {
/*  88:133 */     if (level == 1) {
/*  89:134 */       Configuration.logWarning(msg);
/*  90:135 */     } else if (level == 2) {
/*  91:136 */       if (Configuration.isInfoLoggingEnabled()) {
/*  92:136 */         Configuration.logInfo(msg);
/*  93:138 */       } else if (Configuration.isDebugLoggingEnabled()) {
/*  94:138 */         Configuration.logDebug(msg);
/*  95:    */       }
/*  96:    */     }
/*  97:    */   }
/*  98:    */   
/*  99:    */   private synchronized void addXAResourceTransaction(XAResourceTransaction restx, XAResource xares)
/* 100:    */   {
/* 101:145 */     this.xaresToTxMap_.put(new XAResourceKey(xares), restx);
/* 102:    */   }
/* 103:    */   
/* 104:    */   private synchronized XAResourceTransaction findXAResourceTransaction(XAResource xares)
/* 105:    */   {
/* 106:152 */     XAResourceTransaction ret = null;
/* 107:153 */     ret = (XAResourceTransaction)this.xaresToTxMap_.get(new XAResourceKey(xares));
/* 108:    */     
/* 109:    */ 
/* 110:156 */     return ret;
/* 111:    */   }
/* 112:    */   
/* 113:    */   private synchronized void removeXAResourceTransaction(XAResource xares)
/* 114:    */   {
/* 115:162 */     this.xaresToTxMap_.remove(new XAResourceKey(xares));
/* 116:    */   }
/* 117:    */   
/* 118:    */   public void registerSynchronization(Synchronization s)
/* 119:    */     throws IllegalStateException, SystemException
/* 120:    */   {
/* 121:    */     try
/* 122:    */     {
/* 123:174 */       Sync2Sync adaptor = new Sync2Sync(s);
/* 124:175 */       this.ct_.registerSynchronization(adaptor);
/* 125:    */     }
/* 126:    */     catch (SysException se)
/* 127:    */     {
/* 128:177 */       String msg = "Unexpected error during registerSynchronization";
/* 129:178 */       Configuration.logWarning(msg, se);
/* 130:179 */       throw new ExtendedSystemException(msg, se.getErrors());
/* 131:    */     }
/* 132:    */   }
/* 133:    */   
/* 134:    */   public int getStatus()
/* 135:    */   {
/* 136:191 */     TxState state = (TxState)this.ct_.getState();
/* 137:193 */     if (state.equals(TxState.IN_DOUBT)) {
/* 138:194 */       return 2;
/* 139:    */     }
/* 140:195 */     if (state.equals(TxState.PREPARING)) {
/* 141:196 */       return 7;
/* 142:    */     }
/* 143:197 */     if (state.equals(TxState.ACTIVE)) {
/* 144:198 */       return 0;
/* 145:    */     }
/* 146:199 */     if (state.equals(TxState.MARKED_ABORT)) {
/* 147:200 */       return 1;
/* 148:    */     }
/* 149:203 */     return 5;
/* 150:    */   }
/* 151:    */   
/* 152:    */   private CompositeTerminator getTerminator()
/* 153:    */   {
/* 154:208 */     TransactionControl control = this.ct_.getTransactionControl();
/* 155:209 */     if (control == null)
/* 156:    */     {
/* 157:210 */       String msg = "No TransactionControl object found?";
/* 158:211 */       Configuration.logWarning(msg);
/* 159:212 */       throw new SecurityException(msg);
/* 160:    */     }
/* 161:215 */     CompositeTerminator term = control.getTerminator();
/* 162:216 */     if (term == null)
/* 163:    */     {
/* 164:217 */       String msg = "No TransactionTerminator object found?";
/* 165:218 */       Configuration.logWarning(msg);
/* 166:219 */       throw new SecurityException(msg);
/* 167:    */     }
/* 168:221 */     return term;
/* 169:    */   }
/* 170:    */   
/* 171:    */   public void commit()
/* 172:    */     throws javax.transaction.RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException, SecurityException
/* 173:    */   {
/* 174:234 */     CompositeTerminator term = getTerminator();
/* 175:    */     try
/* 176:    */     {
/* 177:236 */       term.commit();
/* 178:    */     }
/* 179:    */     catch (HeurHazardException hh)
/* 180:    */     {
/* 181:238 */       rethrowAsJtaHeuristicMixedException(hh.getMessage(), hh);
/* 182:    */     }
/* 183:    */     catch (HeurRollbackException hr)
/* 184:    */     {
/* 185:240 */       rethrowAsJtaHeuristicRollbackException(hr.getMessage(), hr);
/* 186:    */     }
/* 187:    */     catch (HeurMixedException hm)
/* 188:    */     {
/* 189:242 */       rethrowAsJtaHeuristicMixedException(hm.getMessage(), hm);
/* 190:    */     }
/* 191:    */     catch (SysException se)
/* 192:    */     {
/* 193:244 */       Configuration.logWarning(se.getMessage(), se);
/* 194:245 */       throw new ExtendedSystemException(se.getMessage(), se.getErrors());
/* 195:    */     }
/* 196:    */     catch (com.atomikos.icatch.RollbackException rb)
/* 197:    */     {
/* 198:249 */       String msg = rb.getMessage();
/* 199:250 */       rethrowAsJtaRollbackException(msg, rb);
/* 200:    */     }
/* 201:    */   }
/* 202:    */   
/* 203:    */   public void rollback()
/* 204:    */     throws IllegalStateException, SystemException
/* 205:    */   {
/* 206:260 */     CompositeTerminator term = getTerminator();
/* 207:    */     try
/* 208:    */     {
/* 209:262 */       term.rollback();
/* 210:    */     }
/* 211:    */     catch (SysException se)
/* 212:    */     {
/* 213:264 */       Configuration.logWarning(se.getMessage(), se);
/* 214:265 */       throw new ExtendedSystemException(se.getMessage(), se.getErrors());
/* 215:    */     }
/* 216:    */   }
/* 217:    */   
/* 218:    */   public void setRollbackOnly()
/* 219:    */     throws IllegalStateException, SystemException
/* 220:    */   {
/* 221:279 */     this.ct_.getTransactionControl().setRollbackOnly();
/* 222:    */   }
/* 223:    */   
/* 224:    */   public boolean enlistResource(XAResource xares)
/* 225:    */     throws javax.transaction.RollbackException, SystemException, IllegalStateException
/* 226:    */   {
/* 227:290 */     TransactionalResource res = null;
/* 228:291 */     XATransactionalResource xatxres = null;
/* 229:292 */     XAResourceTransaction restx = null;
/* 230:293 */     Stack errors = new Stack();
/* 231:295 */     if (getStatus() == 1)
/* 232:    */     {
/* 233:296 */       String msg = "Transaction is already marked for rollback - enlisting more resources is useless.";
/* 234:297 */       Configuration.logWarning(msg);
/* 235:298 */       throw new javax.transaction.RollbackException(msg);
/* 236:    */     }
/* 237:301 */     Enumeration enumm = Configuration.getResources();
/* 238:303 */     while (enumm.hasMoreElements())
/* 239:    */     {
/* 240:304 */       RecoverableResource rres = (RecoverableResource)enumm.nextElement();
/* 241:306 */       if ((rres instanceof XATransactionalResource))
/* 242:    */       {
/* 243:307 */         xatxres = (XATransactionalResource)rres;
/* 244:309 */         if (xatxres.usesXAResource(xares)) {
/* 245:310 */           res = xatxres;
/* 246:    */         }
/* 247:    */       }
/* 248:    */     }
/* 249:316 */     printMsg("enlistResource ( " + xares + " ) with transaction " + toString(), 2);
/* 250:319 */     if (res == null) {
/* 251:321 */       if (this.autoRegistration_)
/* 252:    */       {
/* 253:323 */         synchronized (Configuration.class)
/* 254:    */         {
/* 255:328 */           res = new TemporaryXATransactionalResource(xares);
/* 256:331 */           if (Configuration.getResource(res.getName()) == null)
/* 257:    */           {
/* 258:332 */             printMsg("constructing new temporary resource for unknown XAResource: " + xares, 3);
/* 259:    */             
/* 260:334 */             Configuration.addResource(res);
/* 261:    */           }
/* 262:    */         }
/* 263:    */       }
/* 264:    */       else
/* 265:    */       {
/* 266:339 */         String msg = "There is no registered resource that can recover the given XAResource instance. \nEither enable automatic resource registration, or register a corresponding resource.";
/* 267:    */         
/* 268:341 */         Configuration.logWarning(msg);
/* 269:342 */         throw new SystemException(msg);
/* 270:    */       }
/* 271:    */     }
/* 272:348 */     XAResourceTransaction active = findXAResourceTransaction(xares);
/* 273:350 */     if (active != null)
/* 274:    */     {
/* 275:354 */       if (!active.isXaSuspended())
/* 276:    */       {
/* 277:355 */         String msg = "The given XAResource instance is being enlisted a second time without delist in between?";
/* 278:356 */         Configuration.logWarning(msg);
/* 279:357 */         throw new IllegalStateException(msg);
/* 280:    */       }
/* 281:    */       try
/* 282:    */       {
/* 283:365 */         active.setXAResource(xares);
/* 284:366 */         active.xaResume();
/* 285:    */       }
/* 286:    */       catch (XAException xaerr)
/* 287:    */       {
/* 288:368 */         if ((100 <= xaerr.errorCode) && (xaerr.errorCode <= 107)) {
/* 289:370 */           rethrowAsJtaRollbackException("Transaction was already rolled back inside the back-end resource. Further enlists are useless.", xaerr);
/* 290:    */         }
/* 291:373 */         errors.push(xaerr);
/* 292:374 */         throw new ExtendedSystemException("Unexpected error during enlist", errors);
/* 293:    */       }
/* 294:    */     }
/* 295:    */     else
/* 296:    */     {
/* 297:    */       try
/* 298:    */       {
/* 299:380 */         restx = (XAResourceTransaction)res.getResourceTransaction(this.ct_);
/* 300:    */         
/* 301:    */ 
/* 302:    */ 
/* 303:    */ 
/* 304:    */ 
/* 305:    */ 
/* 306:    */ 
/* 307:    */ 
/* 308:    */ 
/* 309:390 */         restx.setXAResource(xares);
/* 310:391 */         restx.resume();
/* 311:    */       }
/* 312:    */       catch (ResourceException re)
/* 313:    */       {
/* 314:394 */         Stack nested = re.getDetails();
/* 315:395 */         if ((!nested.empty()) && ((nested.peek() instanceof XAException)))
/* 316:    */         {
/* 317:397 */           XAException xaerr = (XAException)nested.peek();
/* 318:398 */           if ((100 <= xaerr.errorCode) && (xaerr.errorCode <= 107)) {
/* 319:400 */             rethrowAsJtaRollbackException("The transaction was rolled back in the back-end resource. Further enlists are useless.", re);
/* 320:    */           }
/* 321:    */         }
/* 322:404 */         errors.push(re);
/* 323:405 */         throw new ExtendedSystemException("Unexpected error during enlist", errors);
/* 324:    */       }
/* 325:    */       catch (RuntimeException e)
/* 326:    */       {
/* 327:408 */         throw e;
/* 328:    */       }
/* 329:411 */       addXAResourceTransaction(restx, xares);
/* 330:    */     }
/* 331:414 */     return true;
/* 332:    */   }
/* 333:    */   
/* 334:    */   public boolean delistResource(XAResource xares, int flag)
/* 335:    */     throws IllegalStateException, SystemException
/* 336:    */   {
/* 337:425 */     Stack errors = new Stack();
/* 338:    */     
/* 339:427 */     TransactionalResource res = null;
/* 340:428 */     Enumeration enumm = Configuration.getResources();
/* 341:429 */     while (enumm.hasMoreElements())
/* 342:    */     {
/* 343:430 */       RecoverableResource rres = (RecoverableResource)enumm.nextElement();
/* 344:432 */       if ((rres instanceof XATransactionalResource))
/* 345:    */       {
/* 346:433 */         XATransactionalResource xatxres = (XATransactionalResource)rres;
/* 347:434 */         if (xatxres.usesXAResource(xares)) {
/* 348:435 */           res = xatxres;
/* 349:    */         }
/* 350:    */       }
/* 351:    */     }
/* 352:440 */     printMsg("delistResource ( " + xares + " ) with transaction " + toString(), 2);
/* 353:443 */     if (res == null)
/* 354:    */     {
/* 355:444 */       String msg = "There is no registered resource that can recover the given XAResource instance. \nEither enable automatic resource registration, or register a corresponding resource.";
/* 356:    */       
/* 357:446 */       Configuration.logWarning(msg);
/* 358:447 */       throw new SystemException(msg);
/* 359:    */     }
/* 360:451 */     XAResourceTransaction active = findXAResourceTransaction(xares);
/* 361:456 */     if (active == null)
/* 362:    */     {
/* 363:457 */       String msg = "Illegal attempt to delist an XAResource instance that was not previously enlisted.";
/* 364:458 */       Configuration.logWarning(msg);
/* 365:459 */       throw new IllegalStateException(msg);
/* 366:    */     }
/* 367:462 */     if ((flag == 67108864) || (flag == 536870912))
/* 368:    */     {
/* 369:    */       try
/* 370:    */       {
/* 371:465 */         active.suspend();
/* 372:    */       }
/* 373:    */       catch (ResourceException re)
/* 374:    */       {
/* 375:467 */         errors.push(re);
/* 376:468 */         throw new ExtendedSystemException("Error in delisting the given XAResource", errors);
/* 377:    */       }
/* 378:470 */       removeXAResourceTransaction(xares);
/* 379:473 */       if (flag == 536870912) {
/* 380:474 */         setRollbackOnly();
/* 381:    */       }
/* 382:    */     }
/* 383:475 */     else if (flag == 33554432)
/* 384:    */     {
/* 385:    */       try
/* 386:    */       {
/* 387:479 */         active.xaSuspend();
/* 388:    */       }
/* 389:    */       catch (XAException xaerr)
/* 390:    */       {
/* 391:481 */         errors.push(xaerr);
/* 392:482 */         throw new ExtendedSystemException("Error in delisting the given XAResource", errors);
/* 393:    */       }
/* 394:    */     }
/* 395:    */     else
/* 396:    */     {
/* 397:486 */       String msg = "Unknown delist flag: " + flag;
/* 398:487 */       Configuration.logWarning(msg);
/* 399:488 */       throw new SystemException(msg);
/* 400:    */     }
/* 401:490 */     return true;
/* 402:    */   }
/* 403:    */   
/* 404:    */   public boolean equals(Object o)
/* 405:    */   {
/* 406:506 */     if ((o == null) || (!(o instanceof TransactionImp))) {
/* 407:507 */       return false;
/* 408:    */     }
/* 409:508 */     TransactionImp other = (TransactionImp)o;
/* 410:509 */     return this.ct_.isSameTransaction(other.ct_);
/* 411:    */   }
/* 412:    */   
/* 413:    */   public int hashCode()
/* 414:    */   {
/* 415:520 */     return this.ct_.hashCode();
/* 416:    */   }
/* 417:    */   
/* 418:    */   public String toString()
/* 419:    */   {
/* 420:525 */     return this.ct_.getTid().toString();
/* 421:    */   }
/* 422:    */   
/* 423:    */   void suspendEnlistedXaResources()
/* 424:    */     throws ExtendedSystemException
/* 425:    */   {
/* 426:531 */     Iterator xaResourceTransactions = this.xaresToTxMap_.values().iterator();
/* 427:532 */     while (xaResourceTransactions.hasNext())
/* 428:    */     {
/* 429:533 */       XAResourceTransaction resTx = (XAResourceTransaction)xaResourceTransactions.next();
/* 430:    */       try
/* 431:    */       {
/* 432:535 */         resTx.xaSuspend();
/* 433:    */       }
/* 434:    */       catch (XAException e)
/* 435:    */       {
/* 436:537 */         Stack errors = new Stack();
/* 437:538 */         errors.push(e);
/* 438:539 */         throw new ExtendedSystemException("Error in delisting the given XAResource", errors);
/* 439:    */       }
/* 440:    */     }
/* 441:    */   }
/* 442:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\transactions-jta\3.7.0\transactions-jta-3.7.0.jar
 * Qualified Name:     com.atomikos.icatch.jta.TransactionImp
 * JD-Core Version:    0.7.0.1
 */